// pages/teacher/kechenbiao/kechenbiao.js
Page({
  data: {
    weeks: Array.from({length: 20}, (_, i) => `第${i + 1}周`),
    currentWeekIndex: 0,
    days: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    periods: ['1-2节', '3-4节', '5-6节', '7-8节', '9-10节'],
    
    // 教师课程数据
    teacherCourses: [
      {
        id: 1,
        courseName: '高等数学',
        className: '计算机科学1班',
        location: '教学楼A-101',
        day: 0, // 周一
        period: 0, // 1-2节
        weeks: [1, 2, 3, 4, 5, 6, 7, 8]
      },
      {
        id: 2,
        courseName: '高等数学',
        className: '计算机科学2班',
        location: '教学楼A-102',
        day: 1, // 周二
        period: 0, // 1-2节
        weeks: [1, 2, 3, 4, 5, 6, 7, 8]
      },
      {
        id: 3,
        courseName: '线性代数',
        className: '软件工程1班',
        location: '教学楼B-203',
        day: 2, // 周三
        period: 1, // 3-4节
        weeks: [1, 2, 3, 4, 5, 6, 7, 8]
      }
    ],
    
    // 课程表显示数据
    displayTimetable: Array(7).fill().map(() => Array(5).fill(null))
  },
  
  onLoad: function() {
    // 获取当前是第几周
    this.getCurrentWeek();
    
    // 获取课程表数据
    this.fetchTimetableData();
  },
  
  // 获取当前是第几周
  getCurrentWeek: function() {
    // 这里应该从服务器获取当前是第几周
    // 示例中使用模拟数据
    const currentWeek = 1; // 假设当前是第1周
    
    this.setData({
      currentWeekIndex: currentWeek - 1
    });
  },
  
  // 获取课程表数据
  fetchTimetableData: function() {
    console.log('获取教师课表数据');
    
    this.setData({ isLoading: true });
    
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    
    const week = this.data.currentWeek;
    
    // 修改为使用新的教师课表API
    wx.request({
      url: `${getApp().globalData.serverUrl}/api/courses/timetable/teacher`,
      method: 'GET',
      data: {
        week: week
      },
      header: {
        'Authorization': `Bearer ${wx.getStorageSync('token')}`
      },
      success: (res) => {
        wx.hideLoading();
        console.log('获取教师课表数据成功:', res.data);
        
        if (res.data.success) {
          this.setData({
            timetableData: res.data.timetable || [],
            isLoading: false
          });
          
          // 处理课程表显示
          this.processTimetableData();
        } else {
          wx.showToast({
            title: res.data.message || '获取课程表失败',
            icon: 'none'
          });
          this.setData({ isLoading: false });
        }
      },
      fail: (err) => {
        console.log('获取课表请求失败:', err);
        wx.hideLoading();
        
        wx.showToast({
          title: '网络错误，请稍后重试',
          icon: 'none'
        });
        this.setData({ isLoading: false });
      },
      complete: () => {
        wx.stopPullDownRefresh();
      }
    });
  },
  
  // 处理教师课表数据
  processTimetableData: function() {
    console.log('处理教师课表数据:', this.data.timetableData);
    
    // 创建一个空的课程表显示数组
    const displayTimetable = Array(7).fill().map(() => Array(5).fill(null));
    
    // 遍历课程数据，填充到显示数组中
    this.data.timetableData.forEach(course => {
      // 确保day和period在有效范围内
      if (course.day >= 0 && course.day < 7 && course.period >= 0 && course.period < 5) {
        displayTimetable[course.day][course.period] = {
          id: course.id,
          name: course.courseName,
          className: course.className,
          location: course.location,
          startWeek: course.startWeek,
          endWeek: course.endWeek
        };
      }
    });
    
    this.setData({
      displayTimetable: displayTimetable
    });
    
    console.log('处理后的教师课表显示数据:', displayTimetable);
  },
  
  // 切换周次
  changeWeek: function(e) {
    const weekIndex = e.detail.value;
    this.setData({
      currentWeekIndex: weekIndex
    });
    
    // 重新获取课程表数据
    this.fetchTimetableData();
  },
  
  // 查看课程详情
  viewCourseDetail: function(e) {
    const day = e.currentTarget.dataset.day;
    const period = e.currentTarget.dataset.period;
    const course = this.data.displayTimetable[day][period];
    
    if (!course) return;
    
    wx.navigateTo({
      url: `/pages/teacher/kechenbiao/detail/detail?id=${course.id}`
    });
  },
  
  // 导出课表
  exportTimetable: function() {
    wx.showLoading({
      title: '导出中...',
    });
    
    setTimeout(() => {
      wx.hideLoading();
      wx.showModal({
        title: '导出成功',
        content: '课程表已导出，请到系统后台下载',
        showCancel: false
      });
    }, 1500);
  }
})